package com.collectplus.express.evaluate;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.MainActivity;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ShareBean;

import droid.frame.ui.dialog.BaseDialog;
import droid.frame.utils.http.BitmapUtils;
import droid.frame.utils.lang.JsonParser;

/**
 * 服务评价
 * 
 * @author HuXinDan<br>
 *         2016-11-8上午9:27:24
 */
public class EvaluateCompleteActivity extends BaseActivity2 implements OnClickListener {

	private BaseDialog mShareDialog;
	private String orderId;

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		orderId = getIntent().getStringExtra("orderId");
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void findViewById() {
		setContentView(R.layout.evaluate_complete);
		super.findViewById();
		getAppTitle().setCommonTitle("评价完成");
		findViewById(R.id.evaluate_share).setOnClickListener(this);
		int left = getResources().getIdentifier("title_left_switcher","id", getPackageName());
		findViewById(left).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MainActivity.getInstance().setFromBackground(true);
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 分享领卷
		case R.id.evaluate_share:
			AppHttp.getInstance().getAppConfig(this);
			finish();
//			showLoadingDialog(null);
//			AppHttp.getInstance().weiXinShare(orderId + "");
			break;
		default:
			break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			// 微信分享
		case MsgID.weixin_share:
			cancelLoadingDialog();
			final ShareBean share = JsonParser.parse(msg.obj + "", ShareBean.class);
			// 默认的
			if (share == null) {
				showToast("获取分享数据失败");
				return true;
			} else {
				Bitmap picBitmap = BitmapUtils.loadBitmapFromNet(share.getPicUrl());
				share.setPicBitmap(picBitmap);
			}
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mShareDialog = DialogMgr.showWeixinShare((BaseActivity2) getContext(), share, orderId);
				}
			});
			return true;
			// 微信分享回调以后、取消对话框
		case MsgID.weixin_share_close:
			if (mShareDialog != null) {
				mShareDialog.cancel();
			}
			return true;
		}
		return super.handleMessage(msg);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {// 竖屏
			MainActivity.getInstance().setFromBackground(true);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
